package addition.nio;

import org.junit.Test;

import java.nio.ByteBuffer;

public class BufferDemo {

    /*
     * 除（boolean）外的基本类型都拥有 Buffer
     * ByteBuffer
     * IntBuffer
     * LongBuffer
     * ShortBuffer
     * FloatBuffer
     * DoubleBuffer
     *
     * Buffer 重要属性
     *  position: 当且读写位置
     *  limit: 界限
     *  capacity: Buffer 容量
     *  remark: 上一次 position 位置标记
     */

    // 缓冲
    @Test
    public void test1() {
        // 1. 通过 allocate(int) 方法创建 Buffer
        ByteBuffer buf = ByteBuffer.allocate(10);
        System.out.println("====================== allocate() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());

        // 2. 通过 put() 写入数据
        buf.put("chisheng".getBytes());
        System.out.println("====================== put() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());

        // 3. 通过 flip() 切换为读模式
        // The limit is set to the current position and then the position is set to zero
        buf.flip();
        System.out.println("====================== flip() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());

        // 4. 通过 get() 获取内容
        byte b = buf.get();
        System.out.println("====================== get() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());
        System.out.println("read: " + (char) b);

        // 5. 通过 rewind() 重置读写标记
        buf.rewind();
        System.out.println("====================== rewind() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());
        System.out.println("read: " + (char) b);

        // 6. 通过 clear() 清空缓冲区（切换为写模式）
        // The position is set to zero, the limit is set to the capacity
        buf.clear();
        System.out.println("====================== clear() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());
    }

    // 直接缓冲
    @Test
    public void test2() {
        // 创建直接缓冲
        ByteBuffer buf = ByteBuffer.allocateDirect(10);

        // 输出是否为直接缓冲
        boolean direct = buf.isDirect();
        System.out.println(direct);  // true
    }

    // remark
    @Test
    public void test3() {
        ByteBuffer buf = ByteBuffer.allocate(1024);
        buf.put("chisheng is a boy.".getBytes());
        buf.flip();
        System.out.println((char) buf.get());
        System.out.println("====================== get() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());
        buf.mark();
        System.out.println((char) buf.get());
        buf.reset();
        System.out.println("====================== reset() ======================");
        System.out.println("position: " + buf.position());
        System.out.println("limit: " + buf.limit());
        System.out.println("capacity: " + buf.capacity());
    }

}
